﻿using Newtonsoft.Json;
using Sunny.UI.Win32;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinApp.i18n;
using WinApp.Until;
using WinApp.ViewModel;
using static System.Net.Mime.MediaTypeNames;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace WinApp.View
{
    public partial class DownloadList : UserControl
    {
        public DataTable _table;
        public DownloadList()
        {
            InitializeComponent();
            var mainTitle = string.Empty;
            mainTitle = Language.GetLang("downloadTitle1");
            mainTitle += "\r" + Language.GetLang("downloadTitle2");
            this.uiPanel1.Text = mainTitle;

            uiDataGridView1.ColumnHeadersVisible = false;
            uiDataGridView1.RowTemplate.Height = 65;
            uiDataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
        }

        private void DownloadList_Load(object sender, EventArgs e)
        {
            QueryData();
        }

        private void UpdateCellValue(int rowIndex, int columnIndex)
        {
            // Start a new thread
            new Thread(() =>
            {
                Random random = new Random();
                var newValue = random.Next(100); // 生成0到99的随机数;

                // Check if we need to invoke this on UI thread
                this.Invoke((MethodInvoker)delegate
                {
                    uiDataGridView1.Rows[rowIndex].Cells[columnIndex].Value = newValue;
                });
            }).Start();
        }

        public void ResetData()
        {
            _table.Rows.Add("1", 1, "2");
            _table.Rows[0]["DownloadProgress"] = "111";
            //this.uiDataGridView1.DataSource = null;
            this.uiDataGridView1.DataSource = _table;
            this.uiDataGridView1.Refresh();
        }

        public async Task DownloadAllAsync()
        {
            // 获取要修改的行（这里假设我们根据索引获取第一行）


            for (int i = 0; i < _table.Rows.Count; i++)
            {
                DataRow row = _table.Rows[i];
                row["Status"] = "2";
                uiDataGridView1.Refresh();
                //uiDataGridView1.Rows[i].Cells["clOpDown"].Value = ImageHelper.loadImageFromLocalPath(@"FileFolder/Icon/pause.png");
                for (int j = 0; j <= 100; j++)
                {
                    await Task.Delay(100); // wait for 100 milliseconds
                    row["DownloadProgress"] = j.ToString();
                }

            }
        }

        public void QueryData()
        {
            //var tagCourseSessionDto = this.Tag as TagCourseSessionDto;
            //if (tagCourseSessionDto == null)
            //{
            //    UIMessageDialog.ShowMessageDialog(Language.GetLang("notFindTag"), UILocalize.WarningTitle, showCancelButton: false, UIStyle.Orange, false);
            //    return;
            //}

            LoadingHelper.ShowLoadingScreen();

            _table = new DataTable();
            _table.Columns.Add("Title");
            _table.Columns.Add("Status");
            _table.Columns.Add("DownloadProgress");


            List<MyStructureWithLearningStatuOutPutDto> MainList = new List<MyStructureWithLearningStatuOutPutDto>();

            var txt1 = "A1" + Environment.NewLine + "B1";
            _table.Rows.Add(txt1, 1, "0");

            var txt2 = "A2" + Environment.NewLine + "B2";
            _table.Rows.Add(txt2, 0, "0");

            var txt3 = "A3" + Environment.NewLine + "B3";
            _table.Rows.Add(txt3, 0, "0");

            this.uiDataGridView1.DataSource = _table;

            LoadingHelper.CloseForm();
            uiDataGridView1.ClearSelection();
        }

        private void uiDataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridViewRow row = uiDataGridView1.Rows[e.RowIndex];

            if (uiDataGridView1.Columns[e.ColumnIndex].Name == "clTitle")
            {
                if (row.Cells["clStatus"].Value is string && (string)row.Cells["clStatus"].Value == "1")
                {
                    //row.Cells["clDownloadProgress"].Value = "0";

                    row.Cells["clOpDown"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/downLoad.png");
                    row.Cells["clOpDelete"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/delete1.png");
                }
                if (row.Cells["clStatus"].Value is string && (string)row.Cells["clStatus"].Value == "2")
                {
                    //row.Cells["clDownloadProgress"].Value = "0";

                    row.Cells["clOpDown"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/pause.png");
                    row.Cells["clOpDelete"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/delete1.png");
                }
                else
                {
                    //row.Cells["clDownloadProgress"].Value = "0";

                    row.Cells["clOpDown"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/downLoad.png");
                    row.Cells["clOpDelete"].Value = FileHelper.loadImageFromLocalPath(@"FileFolder/Icon/delete1.png");

                }

            }
        }

        private void uiDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //uiDataGridView1.ClearSelection();
        }

        private async void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (uiDataGridView1.Columns[e.ColumnIndex] is DataGridViewImageColumn && e.RowIndex >= 0)
            {

                if (uiDataGridView1.Columns[e.ColumnIndex].Name == "clOpDown")
                {

                }

                if (uiDataGridView1.Columns[e.ColumnIndex].Name == "clOpDelete")
                {
                    //UpdateCellValue(1, 4);
                    //await aaAsync();
                }
            }
        }
    }
}
